#!/usr/bin/env python
#
#   Copyright (C) 2006 Jonathan Saggau                                    
#   saggau@gmail.com                                                      
#                                                                         
#   This program is free software; you can redistribute it and/or modify  
#   it under the terms of the GNU General Public License as published by  
#   the Free Software Foundation; either version 2 of the License, or     
#   (at your option) any later version.                                   
#                                                                         
#   This program is distributed in the hope that it will be useful,       
#   but WITHOUT ANY WARRANTY; without even the implied warranty of        
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
#   GNU General Public License for more details.                          
#                                                                         
#   You should have received a copy of the GNU General Public License     
#   along with this program; if not, write to the                         
#   Free Software Foundation, Inc.,                                       
#   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  
#
"""Generate a bunch of trigger  in bulk"""
import sys, os   
from buildtxt import Buildtxt
import deftxt

DEFAULT_PATH = os.getcwd()

class Generator(Buildtxt):
    scLangPath = os.path.expanduser("~/sclang/")

    def __init__(self):
        Buildtxt.__init__(self)
        
    def _generate(self, fn, path=None):
        for channels in deftxt.args.keys(): #mono (1), stereo (2)
            self.setChannels(channels)
            for synthtype in deftxt.synths.keys(): #static, dynamic
                self.setSynthType(synthtype)
                for envtype in deftxt.envs.keys(): #percussive,
                    self.setEnvType(envtype)
                    for olap in deftxt.overlaps:
                        self.setOverlap(olap)
                        if path is not None:
                            fn(path, Buildtxt.getString(self))
                        else:
                            fn(Buildtxt.getString(self))
        
    def _write(self, path, text):
        if not os.path.exists(path):
            usePath = DEFAULT_PATH
            print 
        else:
            usePath = path
        outFile = os.path.join(usePath, Buildtxt.synthName(self) + ".txt")
        if os.path.isfile(outFile):
                 print "removing old file"
                 os.remove(outFile)
        file = open(outFile, 'w')
        print "Writing synthdef %s" %outFile
        file.write(text)
        file.close()

    def _pysclangGenerate(self, text):
        print text
        print
        from jspysclang import wrapper
        lang = wrapper.Wrapper(self.scLangPath)
        lang.run(text)
        # should writeDefFile

    def writeTxt(self, path):
        self._generate(self._write, path)
        
    def writeDef(self):
        self._generate(self._pysclangGenerate)

def main():
    print """This Generates trigger synthdefs"""

    if '-h' in sys.argv:
        help = """ Usage: %s [/path/to/directory/for/files] """ % sys.argv[0]
        sys.exit()

    dir = ""
    if len(sys.argv) == 2 and os.path.exists(sys.argv[1]):
        dir = sys.argv[1]
        
    gen = Generator()
    gen.writeTxt(dir)
    gen.writeDef()
    
if __name__ == "__main__":
    main()